"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiParamsParam = exports.ApiBodyParam = exports.ApiQueryParam = exports.ApiBody = exports.ApiQuery = exports.ApiCustomDelete = exports.ApiCustomPut = exports.ApiCustomPost = exports.ApiCustomGet = exports.ApiDelete = exports.ApiPut = exports.ApiPost = exports.ApiGet = exports.ApiController = exports.ApiModelProperty = exports.ApiModel = void 0;
/**
 * 装饰器
*/
var Meta_1 = require("./Meta");
var index_1 = __importDefault(require("./index"));
var router_1 = require("../router");
var DataType_1 = require("./DataType");
/**
 * 实体类
*/
function ApiModel(options) {
    return function (constructor) {
        var modelMeta = (0, Meta_1.getModelMeta)(constructor);
        modelMeta.name = options.name;
        modelMeta.description = options.description;
        if (typeof options.isCreateTag === 'boolean') {
            modelMeta.isCreateTag = options.isCreateTag;
        }
        if (typeof options.isCreateModel === 'boolean') {
            modelMeta.isCreateModel = options.isCreateModel;
        }
        index_1.default.ModelMetas.push(modelMeta);
    };
}
exports.ApiModel = ApiModel;
/**
 * 实体类属性
*/
function ApiModelProperty(options) {
    return function (target, propertyKey) {
        var modelMeta = (0, Meta_1.getModelMeta)(target.constructor);
        if (!options.property) {
            options.property = propertyKey;
        }
        modelMeta.modelPropertyMetaList.push(options);
    };
}
exports.ApiModelProperty = ApiModelProperty;
/**
 * 接口路径
*/
function ApiController(options) {
    var midwares = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        midwares[_i - 1] = arguments[_i];
    }
    if (!options.path.startsWith('/')) {
        throw new Error('请输入 / 开头的路径');
    }
    return function (constructor) {
        // 调用路由功能
        router_1.Controller.apply(void 0, __spreadArray([options.path], midwares, false))(constructor);
        var pathMeta = (0, Meta_1.getPathMeta)(constructor);
        pathMeta.path = options.path;
        pathMeta.tags = options.tags || [];
        index_1.default.PathMetas.push(pathMeta);
    };
}
exports.ApiController = ApiController;
/**
 * 接口方法
*/
function ApiOperationBuilder(httpMethod, RouterMethod) {
    return function (options) {
        var midwares = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            midwares[_i - 1] = arguments[_i];
        }
        if (!options.path.startsWith('/')) {
            throw new Error('请输入 / 开头的路径');
        }
        return function (target, methodName) {
            // 默认解析成 json
            options.dataFormat = options.dataFormat || router_1.DataFormat.JSON;
            // 调用路由功能
            RouterMethod.apply(void 0, __spreadArray([options.path, options.dataFormat], midwares, false))(target, methodName);
            var operationMeta = (0, Meta_1.getOperationMeta)(target, methodName);
            operationMeta.path = options.path;
            operationMeta.summary = options.summary;
            operationMeta.description = options.description;
            operationMeta.httpMethod = httpMethod;
            operationMeta.description = options.description || '';
            operationMeta.dataFormat = options.dataFormat;
            if (options.parameterList) {
                operationMeta.parameterList = operationMeta.parameterList.concat(options.parameterList);
            }
            if (options.parameterModel) {
                operationMeta.parameterModel = options.parameterModel;
            }
            if (options.responseList) {
                operationMeta.responseList = options.responseList;
            }
            if (options.responseModel) {
                operationMeta.responseModel = options.responseModel;
            }
        };
    };
}
exports.ApiGet = ApiOperationBuilder('get', router_1.Get);
exports.ApiPost = ApiOperationBuilder('post', router_1.Post);
exports.ApiPut = ApiOperationBuilder('put', router_1.Put);
exports.ApiDelete = ApiOperationBuilder('delete', router_1.Delete);
exports.ApiCustomGet = ApiOperationBuilder('get', router_1.CustomGet);
exports.ApiCustomPost = ApiOperationBuilder('post', router_1.CustomPost);
exports.ApiCustomPut = ApiOperationBuilder('put', router_1.CustomPut);
exports.ApiCustomDelete = ApiOperationBuilder('delete', router_1.CustomDelete);
/**
 * 接口参数
*/
function ApiModelParamBuilder(ParamMethod) {
    return function (options) {
        return function (target, methodName, paramIndex) {
            // 调用路由功能
            ParamMethod(target, methodName, paramIndex);
            var operationMeta = (0, Meta_1.getOperationMeta)(target, methodName);
            // model 参数默认 type object
            if (!options || !options.type) {
                options = Object.assign(options || {}, { type: DataType_1.DataType.Object });
            }
            var paramMeta = (0, Meta_1.getParamMeta)(options);
            operationMeta.parameterModel = paramMeta;
        };
    };
}
function ApiSingleParamBuilder(ParamMethod, inValue) {
    return function (options) {
        if (!options.name) {
            throw new Error('请输入参数名称');
        }
        return function (target, methodName, paramIndex) {
            // 调用路由功能
            ParamMethod(options.name)(target, methodName, paramIndex);
            var operationMeta = (0, Meta_1.getOperationMeta)(target, methodName);
            // 单个 参数默认 type string
            var paramMeta = (0, Meta_1.getParamMeta)(options);
            if (inValue) {
                paramMeta.in = inValue;
            }
            operationMeta.parameterList.push(paramMeta);
        };
    };
}
exports.ApiQuery = ApiModelParamBuilder(router_1.Query);
exports.ApiBody = ApiModelParamBuilder(router_1.Body);
exports.ApiQueryParam = ApiSingleParamBuilder(router_1.QueryParam, 'query');
exports.ApiBodyParam = ApiSingleParamBuilder(router_1.BodyParam, 'formData');
exports.ApiParamsParam = ApiSingleParamBuilder(router_1.ParamsParam, 'path');
